<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>修改资讯信息</title>
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}" media="all">
    <script type="text/javascript" th:src="@{/static/js/jquery-3.js}"></script>
    <script th:src="@{/static/layui/layui.js}" charset="utf-8"></script>

    <link rel="stylesheet" type="text/css" th:href="@{/static/simditor/css/styles/simditor.css}" />
    <script type="text/javascript" th:src="@{/static/simditor/js/scripts/jquery.min.js}"></script>
    <script type="text/javascript" th:src="@{/static/simditor/js/scripts/module.js}"></script>
    <script type="text/javascript" th:src="@{/static/simditor/js/scripts/hotkeys.js}"></script>
    <script type="text/javascript" th:src="@{/static/simditor/js/scripts/uploader.js}"></script>
    <script type="text/javascript" th:src="@{/static/simditor/js/scripts/simditor.js}"></script>
</head>
<body>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>修改</legend>
</fieldset>

<div class="layui-form-item">
    <label class="layui-form-label">资讯编号&nbsp;:</label>
    <div class="layui-input-block">
        <input th:value="${seek.number}" readonly type="text" name='number' id="number" autocomplete="off"  style="width:1000px;"   class="layui-input" >
    </div>
</div>

<div class="layui-form-item">
    <label class="layui-form-label">分类名称&nbsp;:</label>
    <div class="layui-input-block">
        <select name="classify" id="category" style="width:1000px; height: 38px;" lay-verify="required" class="layui-form-selectup">
            <option value="">全部分类</option>
            <th:block th:each="sort:${sortList}">
                <option th:value="${sort.name}" th:selected="${sort.name == seek.sortname}">[[${sort.name}]]</option>
            </th:block>
        </select>
    </div>
</div>

<div class="layui-form-item">
    <label class="layui-form-label">标题&nbsp;:</label>
    <div class="layui-input-block">
        <input th:value="${seek.title}" type="text" name='title' id="title" lay-verify="required" lay-reqtext="标题为必填项" placeholder="请输入标题" autocomplete="off"  style="width:1000px;"   class="layui-input" >
    </div>
</div>

<div class="layui-form-item">
    <label class="layui-form-label">关键词&nbsp;:</label>
    <div class="layui-input-block">
        <input th:value="${seek.keyword}" type="text" name='keyword' id="keyword" lay-verify="required" lay-reqtext="关键词为必填项" placeholder="请输入关键词" autocomplete="off"  style="width:1000px;"   class="layui-input" >
    </div>
</div>

<div class="layui-form-item">
    <label class="layui-form-label">内容简介&nbsp;:</label>
    <div class="layui-input-block">
        <textarea th:utext="${seek.intro}" type="text" name='intro' id="intro" lay-verify="required" placeholder="请输入简介" autocomplete="off"  style="width:1000px;" class="layui-textarea"></textarea>
    </div>
</div>


<div class="layui-form-item">
    <label class="layui-form-label">内容详情&nbsp;:</label>
    <div class="layui-input-block">
        <textarea th:utext="${seek.typees == 0 ? seek.content:seek.background}" name='editor' id="editor" placeholder="" autofocus></textarea>
    </div>
</div>

<div class="layui-form-item">
    <label class="layui-form-label">简图&nbsp;:</label>
    <div class="layui-input-block">
        <img th:src="${seek.imgpath}" name='imgpath' id="imgpath"  style="width:100px; height:80px"/>
    </div>
</div>

<div class="layui-form-item">
    <label class="layui-form-label">类型&nbsp;:</label>
    <div class="layui-input-block">
        <select name="type" id="type" style="width:1000px; height: 38px;" disabled="disabled" >
            <option value="">------请选择类型------</option>
            <option value="0" th:selected="${seek.typees == 0}">文章</option>
            <option value="1" th:selected="${seek.typees == 1}">问题</option>
        </select>
    </div>
</div>

<div class="layui-form-item">
    <div class="layui-input-block">
        <button type="submit" class="layui-btn" lay-submit="" lay-filter="updateBtn">修改</button>
    </div>
</div>

<script>

    var tools =[
        'title', 'bold','italic', 'underline', 'strikethrough', 'fontScale'
        , 'color', 'ol', 'ul', 'blockquote', 'code', 'table', 'link'
        , 'image', 'hr', 'indent', 'outdent', 'alignment'
    ];
    var editor = new Simditor({
        textarea: $("#editor"),
        toolbar: tools,
        toolbarFloat: true,
        toolbarFloatOffset: 0,
        toolbarHidden: false,
        upload: {
            url:'/zx/upload',
            params: null,
            fileKey: "editorPic",
            connectionCount: 1,
            leaveConfirm: '正在上传, 你确定要离开吗?'
        }
    });
</script>

<script>
    layui.use(['form'], function () {
        var thisIndex = parent.layer.getFrameIndex(window.name);
        var form = layui.form;
        var parentLayer = parent.layer;
        var $ = layui.$;

        form.on("submit(updateBtn)",function () {
            var content = editor.getValue();
            var typees = $("#type").val();
            var obj = {
                number:$("#number").val(),
                sortname:$("#category").val(),
                title:$("#title").val(),
                keyword:$("#keyword").val(),
                intro:$("#intro").val(),
                typees:typees
            };
            if(typees == 0){
                obj.content = content;
            }else if(typees == 1){
                obj.background = content;
            }

            //获取内容中的图片
            var $div = document.createElement("div");//创建一个div元素对象
            $div.innerHTML = content;//往div里填充html
            var $v = $($div);//从dom对象转换成jquery对象
            var imageList = [];
            var index = 0;
            $.each($v.find("img"),function (v,i) {//选择器找到img元素，循环获取src值，并将获取的图片路径存入数组中
                imageList[index] = i.src;
                console.log("src======"+i.src);
            });
            var imgPath = $("#imgpath").src;
            console.log("---------------------------");
            console.log(imgPath);
            if(imgPath != imageList[0]){
                obj.imgpath = imageList[0];//获取第一个图片路径存入数据库中
            }else{
                obj.imgpath = imgPath;
            }

            $.post("/message/edit",obj,function (data) {
                if(data.code == 9999){
                    parentLayer.msg("修改失败",{
                        icon: 1,
                        time: 1000
                    });
                }else{
                    parentLayer.msg("修改成功",{
                        icon: 1,
                        time: 2000
                    });
                }
                parent.location.reload();
                parentLayer.close(thisIndex);
            },"json");

        })


    })
</script>

</body>
</html>